package com.moonshineproject.view
{
	import com.moonshineproject.GlobalEventDispatcher;
	import com.moonshineproject.IDEModel;
	import com.moonshineproject.editor.BasicTextEditor;
	import com.moonshineproject.events.CloseTabEvent;
	import com.moonshineproject.view.divider.IDEHDividedBox;
	import com.moonshineproject.view.tabview.TabEvent;
	import com.moonshineproject.view.tabview.TabView;
	
	import flash.display.DisplayObject;
	
	import mx.binding.utils.BindingUtils;
	import mx.containers.VBox;
	import mx.core.FlexGlobals;
	import mx.events.CollectionEvent;

	// TODO: Make this an all-in-one flexible layout thing
	public class MainView extends VBox
	{
		private var mainContent:TabView;
		private var mainPanel:IDEHDividedBox;
		private var model:IDEModel;
		
		public function MainView()
		{
			super();
			
			model = IDEModel.getInstance();
			model.editors.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleEditorChange);
			BindingUtils.bindSetter(activeEditorChanged, model, 'activeEditor');
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			setStyle('verticalGap', 0);
			
			mainPanel = new IDEHDividedBox();
			mainPanel.percentWidth = 100;
			mainPanel.percentHeight = 100;
			//firstRow.setStyle('horizontalGap', 0);
			mainPanel.setStyle('dividerThickness', 2);
			mainPanel.setStyle('dividerAffordance', 2);
			mainPanel.setStyle('horizontalGap', 2);
			addChild(mainPanel);
			
			mainContent = new TabView();
			mainContent.styleName = "tabNav";
			mainContent.percentWidth = 100;
			mainContent.percentHeight = 100;
			mainContent.addEventListener(TabEvent.EVENT_TAB_CLOSE, handleTabClose);
			mainContent.addEventListener(TabEvent.EVENT_TAB_SELECT, focusNewEditor);
			mainPanel.addChild(mainContent);
		}
		
		protected function handleEditorChange(event:CollectionEvent):void
		{
			switch (event.kind)
			{
				case 'remove':
				{
					var editor:DisplayObject = event.items[0] as DisplayObject;
					mainContent.removeChild(editor);
					break;
				}
				case 'add':
				{
					editor = model.editors.getItemAt(event.location) as DisplayObject;
					mainContent.addChild(editor);
					mainContent.selectedIndex = mainContent.getChildIndex(editor);
					model.activeEditor = editor as IContentWindow;
					break;
				}
					
			} 
		}
		
		protected function focusNewEditor(event:TabEvent):void
		{
			if (event.child is IContentWindow)
			{
				model.activeEditor = event.child as IContentWindow;
			}
		}
		
		protected function activeEditorChanged(newActiveEditor:IContentWindow):void
		{
			if (!mainContent) return;
			var childIndex:int = mainContent.getChildIndex(model.activeEditor as DisplayObject);
			if (childIndex != mainContent.selectedIndex) 
			{
				mainContent.selectedIndex = childIndex;
			}
			
			var editorLabel:String = newActiveEditor.label;
			FlexGlobals.topLevelApplication.title = "Moonshine — " + newActiveEditor.longLabel;
		}
		
		public function handleTabClose(event:TabEvent):void
		{
			// We handle this by ourselves.
			event.preventDefault(); 
			
			var e:CloseTabEvent = new CloseTabEvent(event.child);
			GlobalEventDispatcher.getInstance().dispatchEvent(e);
		}
		
		public function addPanel(panel:IPanelWindow):void
		{
			// if (mainPanel.numChildren > 1) reparent & wrap-in-vdivider
			mainPanel.addChild(panel as DisplayObject);
		}
		
		public function removePanel(panel:IPanelWindow):void
		{
			mainPanel.removeChild(panel as DisplayObject);
		}
		
		private function addEditor(editor:IContentWindow):void 
		{
			mainContent.addChild(editor as DisplayObject);
			mainContent.selectedIndex = mainContent.getChildIndex(editor as DisplayObject);
		}
		
		private function replaceEditorWith(newEditor:IContentWindow):void 
		{	
			mainContent.removeChildAt( mainContent.selectedIndex );
			mainContent.addChild(newEditor as DisplayObject);
		}
		
	}
}